package com.duowei.diandan.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.food.FoodImpl;
import com.duowei.diandan.callback.FoodImplDiffCallback;
import com.duowei.diandan.databinding.CommonFoodItemBinding;
import com.duowei.diandan.util.FoodHelper;

import java.util.List;

/**
 * 通用食物Adapter
 */
public class CommonFoodAdapter extends RecyclerView.Adapter<CommonFoodAdapter.ViewHolder> {
    private List<? extends FoodImpl> mFoodList;
    private int mColumnNum = -1;

    /**
     * LinearLayoutManager使用
     *
     * @param foodList  食物列表
     * @param columnNum 列数
     */
    public CommonFoodAdapter(List<? extends FoodImpl> foodList, int columnNum) {
        mFoodList = foodList;
        mColumnNum = columnNum;
    }

    /**
     * GridLayoutManager使用
     *
     * @param foodList
     */
    public CommonFoodAdapter(List<? extends FoodImpl> foodList) {
        mFoodList = foodList;
    }

    public void update(List<? extends FoodImpl> foodList) {
        FoodImplDiffCallback callback = new FoodImplDiffCallback(mFoodList, foodList);
        mFoodList = foodList;
        DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
        result.dispatchUpdatesTo(this);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        CommonFoodItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.item_common_food, parent, false);

        if (mColumnNum > 0) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    parent.getWidth() / mColumnNum, RelativeLayout.LayoutParams.WRAP_CONTENT);
            binding.frameLayout.setLayoutParams(params);
        }

        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CommonFoodItemBinding binding = holder.mBinding;
        FoodImpl food = mFoodList.get(holder.getAdapterPosition());

        if (binding.include != null) {

            FoodHelper.setCoverImage(food.getImgNo(), binding.include.coverView);
            FoodHelper.setPrice(food, binding.include.priceDescView);
            binding.include.nameView.setText(food.getName());

            binding.getRoot().setOnClickListener(v -> {
                onItemClick(food, holder.getAdapterPosition());
            });

            setupView(binding, food);

            binding.executePendingBindings();
        }
    }

    @Override
    public int getItemCount() {
        return mFoodList.size();
    }

    protected void setupView(CommonFoodItemBinding binding, FoodImpl food) {

    }

    protected void onItemClick(FoodImpl food, int position) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(food);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        CommonFoodItemBinding mBinding;

        public ViewHolder(CommonFoodItemBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(FoodImpl food);
    }
}
